package com.example.rapichofer;

import utils.Constantes;

import com.google.gson.Gson;

import entidades.Solicitud;
import entidades.Viaje;

import activitis.MainActivity;
import activitis.RespuestaViajeActivity;
import activitis.TurnoActivity;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;

public class GcmIntentService extends IntentService
{
	public static final int NOTIFICATION_ID = 1;

	public GcmIntentService()
	{
		super("GcmIntentService");
	}

	@Override
	protected void onHandleIntent(Intent intent)
	{
		Bundle extras = intent.getExtras();
		
		if (!extras.isEmpty())
		{
			String tipo = extras.getString("tipo");
			
			switch (tipo)
			{
			case Constantes.TipoGCM.SOLICITUD_VIAJE:
				Solicitud solicitud = new Gson().fromJson(extras.getString("object"), Solicitud.class);
				sendNotification(solicitud);
				break;
			case Constantes.TipoGCM.VIAJE_CANCELADO:
				notificar("RapiTaxi", "El viaje ha sido cancelado.", TurnoActivity.class);
				break;
			case Constantes.TipoGCM.VIAJE_FINALIZADO:
				notificar("RapiTaxi", "El viaje ha finalizado.", TurnoActivity.class);
				break;
			}
		}
		
		ReceptorLocal.completeWakefulIntent(intent);
	}

	private void sendNotification(Solicitud solicitud)
	{
		Viaje viaje = solicitud.getViaje();
		Uri sonido = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
	    .setSmallIcon(R.drawable.ic_launcher)
	    .setContentTitle("RapiTaxi")
	    .setContentText(viaje.getPuntos().get(0).getDireccion())
	    .setSound(sonido);
		
		Intent resultIntent = new Intent(this, RespuestaViajeActivity.class);
		TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
		stackBuilder.addParentStack(MainActivity.class); 
		stackBuilder.addNextIntent(resultIntent);
	
		resultIntent.putExtra("solicitud", new Gson().toJson(solicitud));
		
		PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
		                                    0, PendingIntent.FLAG_UPDATE_CURRENT );
		mBuilder.setContentIntent(resultPendingIntent);
		mBuilder.setAutoCancel(true);
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		mBuilder.setAutoCancel(true);
		mNotificationManager.notify(1, mBuilder.build());
	}
	
	private void notificar (String titulo, String mensaje, Class<?> activity)
	{
		Uri sonido = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
	    .setSmallIcon(R.drawable.ic_launcher)
	    .setContentTitle(titulo)
	    .setContentText(mensaje)
	    .setSound(sonido);
		
		Intent resultIntent = new Intent(this, activity);
		TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
		stackBuilder.addParentStack(MainActivity.class); 
		stackBuilder.addNextIntent(resultIntent);
		
		PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
		                                    0, PendingIntent.FLAG_UPDATE_CURRENT );
		mBuilder.setContentIntent(resultPendingIntent);
		mBuilder.setAutoCancel(true);
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		mNotificationManager.notify(1, mBuilder.build());
	}
}
